package cn.edu.seu.cose.qol.ws.resource.questionnaire;

import java.util.Map;

import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import cn.edu.seu.cose.qol.model.dto.question.QuestionBaseInstance;
import cn.edu.seu.cose.qol.model.dto.question.QuestionInstance;
import cn.edu.seu.cose.qol.ws.app.RequestAttributes;
import cn.edu.seu.cose.qol.ws.resource.QolServerResource;
import cn.edu.seu.cose.qol.ws.resource.questionnaire.strategy.QuestionBaseStrategy;
import cn.edu.seu.cose.qol.ws.resource.questionnaire.strategy.QuestionBaseStrategyFactory;
import cn.edu.seu.cose.qol.ws.resource.util.DomRepresentationBuilder;
import cn.edu.seu.cose.qol.ws.resource.util.XmlObjectReader;

public class QuestionBaseInstanceResource extends QolServerResource {

	@Get("xml")
	public Representation getQuestionBaseInstance() {
		try {
			String questionnaireName = getQuestionnaireName();
			String questionBaseName = getQuestionBaseName();
			
			QuestionBaseStrategyFactory factory =
					QuestionBaseStrategyFactory.genInstance();
			QuestionBaseStrategy strategy =
					factory.getQuestionBaseStrategyByName(questionBaseName);
			
			QuestionBaseInstance instance =
					strategy.getQuestionBase(questionnaireName);
			DomRepresentationBuilder builder  =
					DomRepresentationBuilder.getInstance();
			return builder.getRepresentation(instance);
		} catch (Exception e) {
			return getRepresentationByException(e);
		}
	}
	
	@Post("xml:xml")
	public Representation createNewQuestion(
			Representation representation) {
		try {
			String questionnaireName = getQuestionnaireName();
			String questionBaseName = getQuestionBaseName();
			
			DomRepresentation domRepresentation =
					new DomRepresentation(representation);
			XmlObjectReader reader = XmlObjectReader.getInstance();
			QuestionInstance questionInstance = (QuestionInstance)
					reader.getXmlObject(domRepresentation,
							QuestionInstance.class);
			
			QuestionBaseStrategyFactory factory =
					QuestionBaseStrategyFactory.genInstance();
			QuestionBaseStrategy strategy =
					factory.getQuestionBaseStrategyByName(questionBaseName);
			
			strategy.createNewQuestion(questionInstance, questionnaireName);
			return getSuccessRepresentation();
		} catch (Exception e) {
			return getRepresentationByException(e);
		}
	}
	
	@Delete("xml:xml")
	public Representation removeAllQuestions(
			Representation representation) {
		return null;
	}
	
	private String getQuestionnaireName() {
		Map<String, Object> requestAttributes = getRequestAttributes();
		return (String) requestAttributes.get(
				RequestAttributes.QUESTIONNAIRE_NAME);
	}
	
	private String getQuestionBaseName() {
		Map<String, Object> requestAttributes = getRequestAttributes();
		return (String) requestAttributes.get(
				RequestAttributes.QUESTION_BASE_NAME);
	}

}
